package zhieasy.cn.xpu_helper.controller.book_market;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhieasy.cn.common.utils.result.ResultUtils;
import zhieasy.cn.common.vo.ResultVO;
import zhieasy.cn.model.book_market.Book;
import zhieasy.cn.model.book_market.BookAndUserBookDTO;
import zhieasy.cn.model.book_market.UserBook;
import zhieasy.cn.service.book_market.BookService;

import javax.servlet.ServletContext;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author wangzeya
 * @create 2020-08-12 21:30:00
 */
@Api(tags = "图书接口", value = "book controller")
@RequestMapping("/book-market")
@RestController
//@ApiImplicitParams({
//        @ApiImplicitParam(name="isbn",value ="国际标准书号",required =true,paramType="form",dataType="String"),
//        @ApiImplicitParam(name="classify",value ="分类",required =true,paramType="form",dataType="String"),
//        @ApiImplicitParam(name="faction",value ="交易类型",required =true,paramType="form",dataType="String"),
//        @ApiImplicitParam(name="book_name",value ="图书名",required =true,paramType="form",dataType="String")
//})
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private ServletContext servletContext;

    /**
     * 匹配图书
     */
    @ApiOperation(value = "匹配图书", notes = "匹配图书")
    @RequestMapping(value = "/bookIsbn/{isbn}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")

    public ResultVO<Book> matchBook(@ApiParam(value = "国际标准书号", required = true) @PathVariable("isbn")  String isbn) {
        return bookService.matchBook(isbn);
    }

    @ApiOperation(value = "帖子是否上传成功" , produces = "application/json;charset=UTF-8")
    @RequestMapping(value = "/isPut/{uuid}",method = RequestMethod.GET)

    public ResultVO isPut(@ApiParam(value = "帖子id，put返回的uuid", required = true) @PathVariable("uuid") String uuid){
        if ((boolean) servletContext.getAttribute(uuid + "flag")){
            return ResultUtils.success("上传成功",(UserBook)servletContext.getAttribute(uuid));
        }else{
            return ResultUtils.error("上传未成功",1000);
        }
    }
    /**
     * 发布图书
     */
    @ApiOperation(value = "发布图书", notes = "发布图书")
    @RequestMapping(value = "/userBook",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ResultVO<Integer> putBook(@RequestBody UserBook userBook) {
        //生成一下唯一的id
        String uuid = UUID.randomUUID().toString();
        userBook.setUbId(uuid);
        return bookService.putBook(userBook);
    }

    /**
     * 修改已发布图书的信息
     */
    @ApiOperation(value = "修改已发布图书的信息", notes = "修改已发布图书的信息")
    @RequestMapping(value = "/userBook/{ubId}",method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
    public ResultVO<Integer> updateBook(@ApiParam(value = "帖子的id", required = true)@PathVariable("ubId") String ubId
        ,@RequestBody UserBook userBook) {
        return bookService.updateBook(ubId, userBook);
    }

    /**
     * 下架图书
     */
    @ApiOperation(value = "下架图书", notes = "下架图书")
    @RequestMapping(value = "/userBook/{ubId}",method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public ResultVO<Integer> deleteBook(@ApiParam(value = "用户图书id", required = true)@PathVariable("ubId")
                                                    String ub_id) {
        return bookService.deleteBook(ub_id);
    }

    /**
     * 查看某个分类的图书
     *
     * @param classify 分类
     * @return
     */
    @ApiOperation(value = "查看某个分类的图书", notes = "查看某个分类的图书")
    @RequestMapping(value = "/book/category/{classify}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    public ResultVO<List<Book>> bookOfClass(@ApiParam(value = "图书分类", required = true) @PathVariable("classify") String classify,
                                            @ApiParam("页数,第几页") @RequestParam int pageIndex,
                                            @ApiParam("页数,第几页") @RequestParam int pageSize) {
        return bookService.bookOfClass(classify,pageIndex,pageSize);
    }

    /**
     * 查看市场上图书
     */
    @ApiOperation(value = "查看市场上图书", notes = "查看市场上图书", httpMethod = "GET")
    @RequestMapping(value = "/userBook/{faction}",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ResultVO<List<UserBook>> allBooks(
        @ApiParam("出售或者租借,在此处填写sell或者borrow字符串")@PathVariable("faction") String faction,
        @ApiParam("页数,第几页") @RequestParam int pageIndex,@ApiParam("页数,第几页") @RequestParam int pageSize) {
        return bookService.allBooks(faction,pageIndex,pageSize);
    }

    /**
     * 查看某个用户的某本书的详细信息(每访问一次点击量+1)
     */
    @ApiOperation(value = "查看某个用户的某本书的详细信息", notes = "查看某个用户的某本书的详细信息")
    @RequestMapping(value = "/bookDetail/{ubId}",method = {RequestMethod.GET,RequestMethod.PATCH}, produces = "application/json;charset=UTF-8")
    public ResultVO<UserBook> singleUserBook(@ApiParam(value = "用户图书id", required = true)@PathVariable("ubId") String ubId) {
        return bookService.singleUserBook(ubId);
    }

    /**
     * 图书推荐
     *
     * @param isbn 图书号
     * @param ubId 图书id
     * @return ResultVO<BookAndUserBookDTO>
     */
    @ApiOperation(value = "图书推荐", notes = "图书推荐")
    @GetMapping(value = "/bookInfo/isbn/{isbn}/ubId/{ubId}", produces = "application/json;charset=UTF-8")
    public ResultVO<BookAndUserBookDTO> recommendBook(@ApiParam(value = "国际标准书号", required = true) @PathVariable("isbn") String isbn,
                                                      @ApiParam(value = "用户图书id", required = true) @PathVariable("ubId") String ubId) {
        return bookService.recommendBook(isbn, ubId);
    }

    /**
     * 查看某种书的详细信息
     */
    @ApiOperation(value = "查看某种书的详细信息", notes = "查看某种书的详细信息")
    @GetMapping(value = "/book/{isbn}", produces = "application/json;charset=UTF-8")
    public ResultVO<Book> singleBook(@ApiParam(value = "国际标准书号", required = true)@PathVariable("isbn") String isbn) {
        return bookService.singleBook(isbn);
    }

    /**
     * 查看我发布的图书
     */
    @ApiOperation(value = "查看我发布的图书", notes = "查看我发布的图书")
    @GetMapping(value = "/book/faction/{faction}/openId/{openId}", produces = "application/json;charset=UTF-8")
    public ResultVO<List<BookAndUserBookDTO>> myBook(@ApiParam(value = "出售或者租借", required = true)@PathVariable("faction") String faction,
                                                     @ApiParam(value = "用户id", required = true) @PathVariable("openId") String openId,
                                                     @ApiParam("页数,第几页") @RequestParam int pageIndex,
                                                     @ApiParam("页数,第几页") @RequestParam int pageSize) {
        return bookService.myBook(faction, openId,pageIndex,pageSize);
    }

    /**
     * 搜索图书
     */
    @ApiOperation(value = "搜索图书", notes = "搜索图书")
    @GetMapping(value = "/book/name/{bookName}", produces = "application/json;charset=UTF-8")
    public ResultVO<List<Book>> searchBook(@ApiParam(value = "书名", required = true) @PathVariable("bookName") String bookName,
                                           @ApiParam("页数,第几页") @RequestParam int pageIndex,
                                           @ApiParam("页数,第几页") @RequestParam int pageSize) {
        return bookService.searchBook(bookName,pageIndex,pageSize);
    }

    /**
     * 查看所有出售此书的人
     */
    @ApiOperation(value = "查看所有出售此书的帖子", notes = "查看所有出售此书的帖子")
    @GetMapping(value = "/book/sell/{isbn}", produces = "application/json;charset=UTF-8")
    public ResultVO allSeller(@ApiParam(value = "国际标准书号", required = true) @PathVariable("isbn") String isbn,
                                                   @ApiParam("页数,第几页") @RequestParam int pageIndex,
                                                   @ApiParam("页数,第几页") @RequestParam int pageSize) {
        return bookService.allSeller(isbn,pageIndex, pageSize);
    }

    /**
     * 查看所有出租此书的人
     */
    @ApiOperation(value = "查看所有出租此书的帖子", notes = "查看所有出租此书的帖子")
    @GetMapping(value = "/book/borrow/{isbn}", produces = "application/json;charset=UTF-8")
    public ResultVO allBorrower(@ApiParam(value = "国际标准书号", required = true) @PathVariable("isbn") String isbn,
                                                     @ApiParam("页数,第几页") @RequestParam int pageIndex,
                                                     @ApiParam("页数,第几页") @RequestParam int pageSize) {
        return bookService.allBorrower(isbn, pageIndex, pageSize);

    }



}
